package com.nsqt.biz.controller;

import com.nsqt.biz.domain.IndustryDO;
import com.nsqt.biz.service.IndustryService;
import com.nsqt.common.utils.PageUtils;
import com.nsqt.common.utils.Query;
import com.nsqt.common.utils.R;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 *
 */
@Controller
@RequestMapping("/biz/industry")
public class IndustryController {
    @Autowired
    private IndustryService industryService;

    @GetMapping()
    @RequiresPermissions("biz:industry:industry")
    String Industry() {
        return "biz/industry/industry";
    }

    @ResponseBody
    @GetMapping("/list")
    @RequiresPermissions("biz:industry:industry")
    public PageUtils list(@RequestParam Map<String, Object> params) {
        //查询列表数据
        Query query = new Query(params);
        List<IndustryDO> industryList = industryService.list(query);
        int total = industryService.count(query);
        PageUtils pageUtils = new PageUtils(industryList, total);
        return pageUtils;
    }

    /**
     * 新增
     */
    @GetMapping("/add")
    @RequiresPermissions("biz:industry:add")
    String add() {
        return "biz/industry/add";
    }

    /**
     * 编辑
     */
    @GetMapping("/edit/{id}")
    @RequiresPermissions("biz:industry:edit")
    String edit(@PathVariable("id") Integer id, Model model) {
        IndustryDO industry = industryService.get(id);
        model.addAttribute("industry", industry);
        return "biz/industry/edit";
    }

    /**
     * 保存
     */
    @ResponseBody
    @PostMapping("/save")
    @RequiresPermissions("biz:industry:add")
    public R save(IndustryDO industry) {
        if (industryService.save(industry) > 0) {
            return R.ok();
        }
        return R.error();
    }

    /**
     * 修改
     */
    @ResponseBody
    @RequestMapping("/update")
    @RequiresPermissions("biz:industry:edit")
    public R update(IndustryDO industry) {
        industryService.update(industry);
        return R.ok();
    }

    /**
     * 删除
     */
    @PostMapping("/remove")
    @ResponseBody
    @RequiresPermissions("biz:industry:remove")
    public R remove(Integer id) {
        if (industryService.remove(id) > 0) {
            return R.ok();
        }
        return R.error();
    }

    /**
     * 批量删除
     */
    @PostMapping("/batchRemove")
    @ResponseBody
    @RequiresPermissions("biz:industry:batchRemove")
    public R remove(@RequestParam("ids[]") Integer[] ids) {
        industryService.batchRemove(ids);
        return R.ok();
    }
}
